<?php
namespace app\web\controller;

use app\model\Theme; 

class Shangjiaadd extends Base
{
    public function Shangjiaadd()
    {
global $_G, $_P;
//load_func('tpl');
$system = $_G['system'];
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);

$op = !empty($_P['op']) ? $_P['op'] : 'display';

if ($op == 'delete') {

    db_delete (t_shangjia, array('id'=>$_P['id']));

	return_tips ( '删除成功！', referer (), 'success' );
}


if ($op == 'tdrenling') {

	db_update(t_team, array('company' => $_P['id']),array('company' => 0 ));
	return_tips ( '团队导入该公司成功！', referer (), 'success' );
}
if ($op == 'grrenling') {

	db_update(t_worker, array('company' => $_P['id']),array('company' => 0 ));
	return_tips ( '员工导入该公司成功', referer (), 'success' );
}
		$do = 'shangjiaadd';
		$shangjiaid = intval ( $_P['shangjiaid'] );
		if ($shangjiaid) {	
			//更新加密串
			// db_update ( t_shangjia, array('jiami'=>time()), array ('id' => $shangjiaid) );

			$shangjia = db_find ( "SELECT y.* FROM " . tablename ( t_shangjia ) . " as y
			 	WHERE y.id= " . $shangjiaid );
			// $quanxian=unserialize($shangjia['quanxian']);
		}
		$sql = "SELECT * FROM " . tablename(t_shangjiagroup) . " 
		order by displayorder";
		$grouplist = db_select($sql);

		// if ($_P['op'] == 'delete') {
		// 	$shangjiaid = intval ( $_P['shangjiaid'] );
		// 	$shangjia = db_find ( "SELECT id FROM " . tablename ( t_shangjia ) . " WHERE id = " . $shangjiaid );
		// 	if (empty ( $shangjia )) {
		// 		message ( '抱歉，信息不存在或是已经被删除！' );
		// 	}
		// 	db_delete ( t_shangjia, array (
		// 			'id' => $shangjiaid
		// 	) );
		// 	message ( '删除成功！', referer (), 'success' );
		// }
		if(empty($shangjia['id'])){
			$shangjia['state']=1;
	        $shangjia['shoppass'] =  20000+rand(0,9999);
	        $shangjia['workerpass'] = 30000+rand(0,9999);
		}else{
			// $ewmurl=$_G['siteroot'].createMobileUrl ('shangjiaweixin',array('id'=>$shangjia['id'],'key'=>$shangjia['jiami']));
		}
 		if (empty($shangjia ['shoppass'])) {
	        $shangjia['shoppass'] =  20000+rand(0,9999);
	    }
 		if (empty($shangjia ['workerpass'])) {
	        $shangjia['workerpass'] = 30000+rand(0,9999);
	    }
	    if (!empty($shangjia['topimgs'])){
	    	$topimgs=explode(';', $shangjia['topimgs']);
		}
		if (checksubmit ()) {
			if (empty($_P['workerpass']) || empty($_P['shoppass'])){
				message ( '密码不可为空', referer (), 'success' );
			}
			
	    	$topimgs = $_P['topimgs'];
			if (!empty($topimgs)) {
				foreach ($topimgs as $key => $value) {
					$topimgs[$key]=tomedia($value);
				}
			}
			$topimgs=implode(';', $topimgs);

			$data = array (
					'displayorder' => intval($_P['displayorder']),
					'groupid' => intval($_P['groupid']),
					'name' => $_P['name'],
					'mobile' => $_P['mobile'],
					'mobile1' => $_P['mobile1'],
					'address' => $_P['address'],
					'logo' => tomedia($_P['logo']),
					'darkicon' => tomedia($_P['darkicon']),
					'text1' => $_P['text1'],
					'text2' => $_P['text2'],
					'text3' => $_P['text3'],
					'text4' => $_P['text4'],
					'topimgs' => $topimgs,
					'video' => $_P['video'],
					'videoyouku' => $_P['videoyouku'],
					'url' => $_P['url'],
					'workerpass' => $_P['workerpass'],
					'shoppass' => $_P['shoppass'],
					'content'=>htmlspecialchars_decode($_P['content']),
					'state' => intval($_P['state']),
					'imgs'  => tomedia($_P['imgs']),
					'xunizhuanfa' => intval($_P['xunizhuanfa']),
					'xuniliulan' => intval($_P['xuniliulan']),
					'xunijinri' => intval($_P['xunijinri']),
					'xunichengjiao' => intval($_P['xunichengjiao']),
			);
			if (! empty ( $shangjiaid )) {
				db_update ( t_shangjia, $data, array (
						'id' => $shangjiaid
				) );
				message ( '更新成功！', referer (), 'success' );
			} else {
				$data ['uniacid'] = $_G['mpid'];
				db_insert ( t_shangjia, $data );
				$shangjiaid = $insertid;
				message ( '添加成功！', referer (), 'success' );
			}
		}
		$return_clicent = get_defined_vars();
return view('/shangjiaadd', $return_clicent);	
}
}